package com.aiss.ws.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiss.ws.R;
import com.aiss.ws.fragment.CurseFragment;
import com.aiss.ws.fragment.CurseFragment_;
import com.aiss.ws.fragment.HomeFragment;
import com.aiss.ws.fragment.HomeFragment_;
import com.aiss.ws.fragment.MyFragment;
import com.aiss.ws.fragment.MyFragment_;
import com.aiss.ws.utils.Command;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;


/**
 * Created by chenqiang on 15/12/26.
 */
@EActivity(R.layout.activity_home)
public class HomeActivity extends BaseActivity {


    /**
     * 首页Fragment
     */
    private HomeFragment homeFragment;

    /**
     * 课程
     */
    private CurseFragment courseFragment;


    /**
     * 我的
     */
    private MyFragment myFragment;


    /**fragment管理器*/
    FragmentManager mFragmentManager;

    /**
     * TAB首页按钮
     */
    @ViewById(R.id.home_lly)
    LinearLayout home_lly;

    @ViewById(R.id.home_img)
    ImageView home_img;

    @ViewById(R.id.home_tv)
    TextView home_tv;

    /**
     * TAB课程按钮
     */
    @ViewById(R.id.course_lly)
    LinearLayout course_lly;

    @ViewById(R.id.course_img)
    ImageView course_img;

    @ViewById(R.id.course_tv)
    TextView course_tv;

    /**
     * TAB我的按钮
     */
    @ViewById(R.id.my_lly)
    LinearLayout my_lly;

    @ViewById(R.id.my_img)
    ImageView my_img;

    @ViewById(R.id.my_tv)
    TextView my_tv;

    /**
     * 当前fragment被填充的布局
     */
    private int index = Command.HOME_FRAGMENT_INDEX;

    /**
     * 首页
     */
    @Click(R.id.home_lly)
    void setHome_lly(){
        setSelecttion(Command.HOME_FRAGMENT_INDEX);
    }

    /**
     * 课程
     */
    @Click(R.id.course_lly)
    void setCourse_lly(){
        setSelecttion(Command.COURSE_FRAGMENT_INDEX);
    }

    /**
     * 我的
     */
    @Click(R.id.my_lly)
    void setMy_lly(){
//        if(Futil.getValue(this,Command.SESSION_KEY,2)!=null&&!Futil.getValue(this,Command.SESSION_KEY,2).toString().equals("")){
            setSelecttion(Command.MY_FRAGMENT_INDEX);
//        }else{
//            RegistActivity_.intent(this).start();
//            showMessage("请登录");
//        }

    }

    public static Activity act;


    @AfterViews
    void init(){
        act = this;
        mFragmentManager = getSupportFragmentManager();
        setSelecttion(index);
    }

    /**
     * 该方法用来保存fragment保存当前的状态
     * @param outState
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("index",index);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        index = savedInstanceState.getInt("index");
        setSelecttion(index);
    }

    /**
     * 界面的切换
     * @param index
     */
    private void setSelecttion(int index){

        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        //隐藏fragment
        hideFragments(transaction);

        switch (index){
            //首页
            case Command.HOME_FRAGMENT_INDEX:
                if(homeFragment==null){
                    homeFragment = new HomeFragment_();
                    transaction.add(R.id.new_main_framelayout,homeFragment);
                } else{
                    transaction.show(homeFragment);
                }
                //图标和字体颜色
                home_img.setImageResource(R.mipmap.tabber_2x01pre);
                course_img.setImageResource(R.mipmap.tabber_2x02);
                my_img.setImageResource(R.mipmap.tabber_2x03);
                home_tv.setTextColor(getResources().getColor(R.color.app));
                course_tv.setTextColor(getResources().getColor(R.color.content));
                my_tv.setTextColor(getResources().getColor(R.color.content));
                break;
            //课程
            case Command.COURSE_FRAGMENT_INDEX:
                if(courseFragment==null){
                    courseFragment = new CurseFragment_();
                    transaction.add(R.id.new_main_framelayout,courseFragment);
                } else{
                    transaction.show(courseFragment);
                }
                //图标和字体颜色
                home_img.setImageResource(R.mipmap.tabber_2x01);
                course_img.setImageResource(R.mipmap.tabber_2x02pre);
                my_img.setImageResource(R.mipmap.tabber_2x03);
                home_tv.setTextColor(getResources().getColor(R.color.content));
                course_tv.setTextColor(getResources().getColor(R.color.app));
                my_tv.setTextColor(getResources().getColor(R.color.content));
                break;
            //我的
            case Command.MY_FRAGMENT_INDEX:
                if(myFragment==null){
                    myFragment = new MyFragment_();
                    transaction.add(R.id.new_main_framelayout,myFragment);
                } else{
                    transaction.show(myFragment);
                }
                //图标和字体颜色
                home_img.setImageResource(R.mipmap.tabber_2x01);
                course_img.setImageResource(R.mipmap.tabber_2x02);
                my_img.setImageResource(R.mipmap.tabber_2x03pre);
                home_tv.setTextColor(getResources().getColor(R.color.content));
                course_tv.setTextColor(getResources().getColor(R.color.content));
                my_tv.setTextColor(getResources().getColor(R.color.app));
                break;

        }

        transaction.commit();


    }

    /**
     * 批量隐藏fragment
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (null != homeFragment) {
            transaction.hide(homeFragment);
        }
        if (null != courseFragment) {
            transaction.hide(courseFragment);
        }
        if(null != myFragment){
            transaction.hide(myFragment);
        }


    }

    /**
     * 双击退出程序
     */
    private int keydown = 1;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(KeyEvent.KEYCODE_BACK==keyCode){

            if(keydown==1){
                keydown=2;
                showMessage(getString(R.string.double_exit));
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                            keydown = 1;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }else if(keydown==2){
                finish();
            }
            return true;
        }



        return super.onKeyDown(keyCode, event);
    }

    public static Activity getActivity(){
        return act;
    }

}
